package toanstt.gameplay;

import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Vibrator;
import android.util.Log;
import android.view.MotionEvent;
import toanstt.com.App;
import toanstt.com.Button;
import toanstt.com.DiamondLinkinActivity;
import toanstt.com.SCR;
import toanstt.com.T;
import toanstt.com.Vec2;
import toanstt.sprites.Sprite;

public class Skill 
{
	int minx,miny,maxx,maxy;
	static public Button but_boom;
	static public Button but_magic;
	static public Button but_boom_n;
	static public Button but_magic_n;
	int temp_x,temp_y;
	int action,actionCode ;
	Vec2 vec2temp;
	int[] x;
	int[] y;
	int n;
	int label_temp;
	int int_temp;
	Sprite explosion;
	Sprite magic;
	boolean ismove=false;
	
	public Skill()
	{
		n=0;
		x = new int[100];
		y = new int[100];
		minx = T.PANEL_MIN_X;
		miny = (int)(T.PANEL_MIN_Y + 13*T.UNIT);
		maxx = T.PANEL_MAX_X;
		maxy = T.PANEL_MAX_Y;
		but_boom = new Button(T.bitmap_button_boom, (int)(minx + 3*T.UNIT) ,(int)( miny+11*T.UNIT), 4*T.UNIT/100);
		but_magic = new Button(T.bitmap_button_magic, (int)(minx + 9*T.UNIT) ,(int)( miny+11*T.UNIT), 4*T.UNIT/100);
		but_boom_n = new Button(T.bitmap_button_boom_n,0,0, 4*T.UNIT/100);
		but_magic_n = new Button(T.bitmap_button_magic_n,0,0, 4*T.UNIT/100);
		but_boom_n.anpla = 150;
		but_magic_n.anpla = 150;
		explosion = new Sprite(T.SPRITES_EXPLOSION, 0, false, false);
		explosion.isdisable = true;
		magic = new Sprite(T.SPRITES_MAGIC, 0, false, false);
		magic.isdisable = true;
	}
	void Add(int x1,int y1)
	{
		if(x1 < 0 || x1 >=11)
			return;
		if(y1 < 0 || y1 >=10)
			return;
		x[n] = x1;
		y[n] = y1;
		n++;
	}
	void DoMagic()
	{
		//if()
		int_temp = CellTable.cell_index[x[0]][y[0]];
		if(int_temp == -2)
			return;
		
		if(int_temp<0 || int_temp >=121)
			return;
		if(CellTable.cell[int_temp] == null)
			return;
		if(CellTable.cell[CellTable.cell_index[x[0]][y[0]]].lable >10 )
			return;
		label_temp = CellTable.cell[int_temp].lable;
		for(int i =1;i<n;i++)
		{
			int_temp = CellTable.cell_index[x[i]][y[i]];
			if(int_temp == -2)
				continue;
			if(CellTable.cell[CellTable.cell_index[x[i]][y[i]]].lable >10)
				continue;
			int_temp= CellTable.cell_index[x[i]][y[i]];
			if(int_temp >=0 && int_temp < CellTable.LENG_ARRAY)
				CellTable.cell[int_temp].lable = label_temp;
		}
		T.PLAY_CURRENT_MANA-=40;
		magic.ReNew();
		magic.SetPos(T.GAMESCREEN_MIN_X + x[0]*T.CELL_WIDTH + T.CELL_RADIUS, T.GAMESCREEN_MIN_Y + y[0]*T.CELL_HEIGHT + T.CELL_RADIUS);
		T.sound.Play(T.sound.MAGIC);
	}
	void DoBoom()
	{
		int_temp = CellTable.cell_index[x[0]][y[0]];
		if(int_temp == -2)
			return;
		for(int i=0;i<n;i++)
		{
			if(CellTable.cell_index[x[i]][y[i]] <0 || CellTable.cell_index[x[i]][y[i]] >=121)
				continue;
			if(CellTable.cell[CellTable.cell_index[x[i]][y[i]]].lable >11)
				CellTable.cell[CellTable.cell_index[x[i]][y[i]]].lable--;
			else
				CellTable.DeleteCellAxis2(x[i], y[i], (int)(T.UNIT/2));
			//Log.d("d","ii");
		}
		
		//T.vibra.DoVibrator((int)(T.UNIT/4));
		
		
		Vibrator v = (Vibrator) DiamondLinkinActivity.context.getSystemService(DiamondLinkinActivity.context.VIBRATOR_SERVICE);
		int dot = 200;      // Length of a Morse Code "dot" in milliseconds
		int dash = 500;     // Length of a Morse Code "dash" in milliseconds
		int short_gap = 200;    // Length of Gap Between dots/dashes
		int medium_gap = 500;   // Length of Gap Between Letters
		int long_gap = 1000;    // Length of Gap Between Words
		long[] pattern = {
		    0,  // Start immediately
		    dot, short_gap, dot, short_gap, dot,    // s
		    medium_gap,
		    dash, short_gap, dash, short_gap, dash, // o
		    medium_gap,
		    dot, short_gap, dot, short_gap, dot,    // s
		    long_gap
		};
		 
		// Only perform this pattern one time (-1 means "do not repeat")
		v.vibrate(pattern, -1);
		T.PLAY_CURRENT_MANA-=10;
		explosion.ReNew();
		explosion.SetPos(T.GAMESCREEN_MIN_X + x[0]*T.CELL_WIDTH + T.CELL_RADIUS, T.GAMESCREEN_MIN_Y + y[0]*T.CELL_HEIGHT + T.CELL_RADIUS);
		T.sound.Play(T.sound.BOOM);
	}
	public void Update()
	{
		if(T.PLAY_CURRENT_MANA<10)
			but_boom.isdisible = true;
		else
		{
			but_boom.isdisible = false;
			but_boom.anpla =255;
		}
		
		
		if(T.PLAY_CURRENT_MANA<40)
			but_magic.isdisible = true;
		else
		{
			but_magic.isdisible = false;
			but_magic.anpla =255;
		}
	}
	void Add(int x1,int y1,int level_index)
	{
		Add(x1,y1);
		switch(level_index)
		{
		case 6:
			Add(x1+2,y1+2);
			Add(x1-2,y1+2);
			Add(x1+2,y1-2);
			Add(x1-2,y1-2);
		case 5:
			Add(x1+2,y1+1);
			Add(x1-2,y1+1);
			Add(x1+2,y1-1);
			Add(x1-2,y1-1);
		case 4:
			Add(x1+1,y1+2);
			Add(x1+1,y1-2);
			Add(x1-1,y1+2);
			Add(x1-1,y1-2);
		case 3:
			Add(x1,y1+2);
			Add(x1,y1-2);
			Add(x1+2,y1);
			Add(x1-2,y1);
		case 2:
			Add(x1+1,y1+1);
			Add(x1+1,y1-1);
			Add(x1-1,y1+1);
			Add(x1-1,y1-1);
		case 1:
			Add(x1,y1+1);
			Add(x1,y1-1);
			Add(x1+1,y1);
			Add(x1-1,y1);
		}
	}
	
	
	public void Draw(Canvas canvas)
	{
		but_boom.Draw(canvas);
		but_magic.Draw(canvas);
		if(T.USE_BOOM == true)
			T.paint.setColor(Color.RED);
		else
			T.paint.setColor(Color.BLUE);
		T.paint.setAlpha(180);
		for(int i =0;i<n;i++)
			canvas.drawRect(T.GAMESCREEN_MIN_X + x[i]*T.CELL_WIDTH, T.GAMESCREEN_MIN_Y + y[i]*T.CELL_HEIGHT, T.GAMESCREEN_MIN_X + (x[i]+1)*T.CELL_WIDTH, T.GAMESCREEN_MIN_Y + (y[i]+1)*T.CELL_HEIGHT, T.paint);
		
		
		if(ismove == true)
		{
			if(T.USE_BOOM == true)
			{
			but_boom_n.ChangePosition(temp_x, temp_y);
			but_boom_n.Draw(canvas);
			}
			else
			if(T.USE_MAGIC == true)
			{
				but_magic_n.ChangePosition(temp_x, temp_y);
				but_magic_n.Draw(canvas);
			}
			//but_magic_n.Draw(canvas);
		}
		
		T.paint.setAlpha(255);
		
		explosion.Draw(canvas, 2*T.PLAY_BOOM_LEVEL*T.CELL_HEIGHT, true);
		magic.Draw(canvas, T.PLAY_MAGIC_LEVEL*T.CELL_HEIGHT, true);
	}
	public void touch(MotionEvent mo)
	{
		if(CellTable.need_update_position>0)
			return;
		action = mo.getAction();
		actionCode = action & MotionEvent.ACTION_MASK;
		temp_x = (int)mo.getX();
		temp_y = (int)mo.getY();
		if(but_boom.check_click(temp_x,temp_y) == true)
		{
			
			if(actionCode == MotionEvent.ACTION_DOWN)
			{
				
				T.USE_BOOM = true;
				
				return;
			}
		}
		else if(but_magic.check_click(temp_x,temp_y) == true)
		{
			if(actionCode == MotionEvent.ACTION_DOWN)
			{
				T.USE_MAGIC = true;
				
				return;
			}
		}
		else
		if(T.USE_BOOM == true)
		{
			if(actionCode == MotionEvent.ACTION_UP)
			{
				ismove = false;
				DoBoom();
				T.USE_BOOM = false;
				n=0;
			}
			else if(actionCode == MotionEvent.ACTION_MOVE)
			{
				ismove = true;
				vec2temp = CellTable.Getcellwithcoor(temp_x, temp_y);
				{
					n=0;
					//Log.d("aaaaa",vec2temp.x + " " +vec2temp.y);
					
						Add(vec2temp.x,vec2temp.y,T.PLAY_BOOM_LEVEL);
				
						
				}
			}
		}
		else
			if(T.USE_MAGIC == true)
			{
				if(actionCode == MotionEvent.ACTION_UP)
				{
					ismove = false;
					DoMagic();
					T.USE_MAGIC = false;
					n=0;
				}
				else if(actionCode == MotionEvent.ACTION_MOVE)
				{
					ismove = true;
					vec2temp = CellTable.Getcellwithcoor(temp_x, temp_y);
					{
						n=0;
						//Log.d("aaaaa",vec2temp.x + " " +vec2temp.y);
						Add(vec2temp.x,vec2temp.y,T.PLAY_MAGIC_LEVEL);
					}
				}
			}
				
	}
}

